import java.util.Scanner;

public class DarrStuTest {
    static public Scanner sc = new Scanner(System.in);
    public static final char SEX[] = {'F', 'M'};
    public static final String CNAME[] = {"一", "二", "三", "四",
                                          "五", "六", "七", "八",
                                          "九", "十", "十一", "十二"};

    // 获取一个整型范围
    static public int [] getRange(int [] inputRange) {
        int [] range = {Integer.MIN_VALUE, Integer.MAX_VALUE};

        if (inputRange.length == 1) {
            range[1] = inputRange[0];
        } else if (inputRange.length > 1) {
            if (inputRange[0] == inputRange[1]) {
                range[0] = inputRange[0];
            } else if (inputRange[0] > inputRange[1]) {
                range[0] = inputRange[1];
                range[1] = inputRange[0];
            } else {
                range[0] = inputRange[0];
                range[1] = inputRange[1];
            }
        }

        return range;
    }

    // 获取随机数
    static public int rand(int ... inputRange) {
        int [] range = getRange(inputRange);

        range[0] = (range[0] < 0) ? 0 : range[0];
        range[1] = (range[1] == Integer.MAX_VALUE) ? range[1] - 1 : range[1];

        // p("start = %d, end = %d\n", range[0], range[1]);

        return (int)(Math.random() * (range[1] - range[0] + 1) + range[0]);
    }

    static public Student randStu(String name) {
        Student stu = new Student(name);

        stu.set("age", (byte)rand(7, 30));
        stu.set("sex", SEX[rand(0, 1)]);
        stu.set("classname", CNAME[rand(0, 11)] + "年级" + rand(1, 10) + "班");

        return stu;
    }

    static public void travel(String prompt, Darr darr) {
        if (darr == null) {
            System.out.printf("%s 没有数组可遍历!\n");
            return;
        }
        System.out.printf("%s", prompt);
        while (darr.next()) {
            ((Student)darr.value()).show("");
        }
    }

    static public void main(String [] args) {
        Darr darr = new Darr();
        String name;

/*
 *         Object s1 = randStu("aa");
 *         Student s2 = randStu("aa");
 *
 *         // s1.set("sex", 'F');
 *         // s1.show("s1: ");
 *         // s2.show("s2: ");
 *
 *         if (s1.equals("aa"))
 *             System.out.printf("相等\n");
 *         else
 *             System.out.printf("不相等\n");
 *
 *         System.exit(0);
 */

        while (true) {
            System.out.printf("请输入学生姓名(q为退出): ");
            name = sc.next();
            if (name.equals("q"))
                break;
            darr.add(randStu(name));
        }

        travel("add travel: \n", darr);

        darr.remove("aa");
        travel("remove travel: \n", darr);

        Student s;
        while (true) {
            System.out.printf("请输入查找学生姓名(q为退出): ");
            name = sc.next();
            if (name.equals("q"))
                break;
            travel("find all travel: \n", darr.find(name, null));
            // travel("find 3 travel: \n", darr.find(name, 3, null));
            travel("find 1 3 travel: \n", darr.find(name, 1, 3, null));
            travel("find -2 3 travel: \n", darr.find(name, -2, 3, null));
        }
    }
}
